<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { apiClient } from '@/api/doctor'
import { workflowAPI } from '@/api/workflow'

const router = useRouter()

// 统计数据
const stats = reactive({
  doctorCount: 0,
  workflowCount: 0,
  fanCount: 0,
  auditCount: 0
})

// 最近活动数据
const recentActivities = ref([
  {
    id: 1,
    icon: '👨‍⚕️',
    title: '新医生注册',
    time: '2分钟前',
    status: 'success',
    statusText: '已完成'
  },
  {
    id: 2,
    icon: '🔍',
    title: '服务审核提交',
    time: '5分钟前',
    status: 'warning',
    statusText: '进行中'
  },
  {
    id: 3,
    icon: '⚙️',
    title: '工作流创建',
    time: '10分钟前',
    status: 'success',
    statusText: '已完成'
  },
  {
    id: 4,
    icon: '👥',
    title: '粉丝关注',
    time: '15分钟前',
    status: 'info',
    statusText: '已处理'
  }
])

// 导航到指定页面
const navigateTo = (path: string) => {
  router.push(path)
}

// 刷新数据
const refreshData = async () => {
  try {
    ElMessage.success('数据刷新成功')
    await fetchStats()
  } catch (error) {
    ElMessage.error('数据刷新失败')
  }
}

// 获取统计数据
const fetchStats = async () => {
  try {
    // 获取医生数量
    const doctorResult = await apiClient.get('/api/DoctorGetDoctorList', {
      params: { pageIndex: 1, pageSize: 1 }
    })
    if (doctorResult.data.code === 100) {
      stats.doctorCount = doctorResult.data.data.totalCount
    }

    // 获取工作流数量
    const workflowResult = await workflowAPI.getWorkflowList({
      pageIndex: 1,
      pageSize: 1
    })
    if (workflowResult.data.code === 100) {
      stats.workflowCount = workflowResult.data.data.totalCount
    }

    // 获取待审核数量（进行中的工作流）
    const auditResult = await workflowAPI.getWorkflowList({
      pageIndex: 1,
      pageSize: 1,
      status: '进行中'
    })
    if (auditResult.data.code === 100) {
      stats.auditCount = auditResult.data.data.totalCount
    }

    // 粉丝数量（这里可以根据实际API调整）
    stats.fanCount = Math.floor(Math.random() * 100) + 50
  } catch (error) {
    console.error('获取统计数据失败:', error)
    // 使用模拟数据
    stats.doctorCount = 25
    stats.workflowCount = 12
    stats.fanCount = 156
    stats.auditCount = 3
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchStats()
})
</script>

<template>
  <div class="home-page">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <h1 class="page-title">医院管理系统</h1>
        <p class="page-subtitle">Hospital Management System</p>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 统计卡片区域 -->
      <div class="stats-section">
        <div class="stats-grid">
          <div class="stat-card">
            <div class="stat-icon">👨‍⚕️</div>
            <div class="stat-content">
              <h3 class="stat-number">{{ stats.doctorCount }}</h3>
              <p class="stat-label">医生总数</p>
            </div>
          </div>
          <div class="stat-card">
            <div class="stat-icon">📋</div>
            <div class="stat-content">
              <h3 class="stat-number">{{ stats.workflowCount }}</h3>
              <p class="stat-label">工作流总数</p>
            </div>
          </div>
          <div class="stat-card">
            <div class="stat-icon">👥</div>
            <div class="stat-content">
              <h3 class="stat-number">{{ stats.fanCount }}</h3>
              <p class="stat-label">粉丝总数</p>
            </div>
          </div>
          <div class="stat-card">
            <div class="stat-icon">✅</div>
            <div class="stat-content">
              <h3 class="stat-number">{{ stats.auditCount }}</h3>
              <p class="stat-label">待审核</p>
            </div>
          </div>
        </div>
      </div>

      <!-- 功能菜单区域 -->
      <div class="menu-section">
        <h2 class="section-title">功能菜单</h2>
        <div class="menu-grid">
          <div class="menu-card" @click="navigateTo('/service-audit')">
            <div class="menu-icon">🔍</div>
            <h3 class="menu-title">服务审核</h3>
            <p class="menu-description">管理医生服务审核流程</p>
            <div class="menu-arrow">→</div>
          </div>

          <div class="menu-card" @click="navigateTo('/workflow')">
            <div class="menu-icon">⚙️</div>
            <h3 class="menu-title">工作流管理</h3>
            <p class="menu-description">查看和管理工作流状态</p>
            <div class="menu-arrow">→</div>
          </div>

          <div class="menu-card" @click="navigateTo('/doctor')">
            <div class="menu-icon">👨‍⚕️</div>
            <h3 class="menu-title">医生管理</h3>
            <p class="menu-description">管理医生信息和列表</p>
            <div class="menu-arrow">→</div>
          </div>

          <div class="menu-card" @click="navigateTo('/fan')">
            <div class="menu-icon">👥</div>
            <h3 class="menu-title">粉丝管理</h3>
            <p class="menu-description">查看医生粉丝列表</p>
            <div class="menu-arrow">→</div>
          </div>

          <div class="menu-card" @click="navigateTo('/doctor-audit')">
            <div class="menu-icon">✅</div>
            <h3 class="menu-title">医生审核</h3>
            <p class="menu-description">审核医生注册申请</p>
            <div class="menu-arrow">→</div>
          </div>

          <div class="menu-card" @click="navigateTo('/instructions')">
            <div class="menu-icon">📋</div>
            <h3 class="menu-title">开单管理</h3>
            <p class="menu-description">管理医疗开单和处方</p>
            <div class="menu-arrow">→</div>
          </div>
        </div>
      </div>

      <!-- 最近活动区域 -->
      <div class="recent-section">
        <h2 class="section-title">最近活动</h2>
        <div class="recent-list">
          <div v-for="activity in recentActivities" :key="activity.id" class="recent-item">
            <div class="activity-icon">{{ activity.icon }}</div>
            <div class="activity-content">
              <h4 class="activity-title">{{ activity.title }}</h4>
              <p class="activity-time">{{ activity.time }}</p>
            </div>
            <div class="activity-status" :class="activity.status">
              {{ activity.statusText }}
            </div>
          </div>
        </div>
      </div>

      <!-- 快速操作区域 -->
      <div class="quick-actions-section">
        <h2 class="section-title">快速操作</h2>
        <div class="quick-actions">
          <el-button type="primary" @click="navigateTo('/service-audit')" class="quick-btn">
            <span class="btn-icon">🔍</span>
            开始审核
          </el-button>
          <el-button type="success" @click="navigateTo('/workflow')" class="quick-btn">
            <span class="btn-icon">⚙️</span>
            创建工作流
          </el-button>
          <el-button type="info" @click="navigateTo('/doctor')" class="quick-btn">
            <span class="btn-icon">👨‍⚕️</span>
            添加医生
          </el-button>
          <el-button type="primary" @click="navigateTo('/billing-management')" class="quick-btn">
            <span class="btn-icon">📋</span>
            开单管理
          </el-button>
          <el-button type="warning" @click="refreshData" class="quick-btn">
            <span class="btn-icon">🔄</span>
            刷新数据
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
  color: white;
}

.page-title {
  font-size: 36px;
  font-weight: bold;
  margin: 0 0 8px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 18px;
  margin: 0;
  opacity: 0.9;
}

.main-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 30px;
}

/* 统计卡片样式 */
.stats-section {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.stat-card {
  display: flex;
  align-items: center;
  padding: 20px;
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  border-radius: 10px;
  color: white;
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
}

.stat-card:nth-child(2) {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-card:nth-child(3) {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-card:nth-child(4) {
  background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
}

.stat-icon {
  font-size: 32px;
  margin-right: 16px;
}

.stat-content h3 {
  margin: 0 0 4px 0;
  font-size: 28px;
  font-weight: bold;
}

.stat-content p {
  margin: 0;
  font-size: 14px;
  opacity: 0.9;
}

/* 菜单区域样式 */
.menu-section {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 24px;
  font-weight: bold;
  margin: 0 0 20px 0;
  color: #333;
}

.menu-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}

.menu-card {
  display: flex;
  align-items: center;
  padding: 24px;
  background: #f8f9fa;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.menu-card:hover {
  background: #e9ecef;
  border-color: #409eff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
}

.menu-icon {
  font-size: 32px;
  margin-right: 16px;
}

.menu-title {
  margin: 0 0 4px 0;
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.menu-description {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.menu-arrow {
  margin-left: auto;
  font-size: 20px;
  color: #409eff;
  font-weight: bold;
}

/* 最近活动样式 */
.recent-section {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.recent-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.recent-item {
  display: flex;
  align-items: center;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  transition: background-color 0.3s ease;
}

.recent-item:hover {
  background: #e9ecef;
}

.activity-icon {
  font-size: 24px;
  margin-right: 16px;
}

.activity-content {
  flex: 1;
}

.activity-title {
  margin: 0 0 4px 0;
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.activity-time {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.activity-status {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.activity-status.success {
  background: #d4edda;
  color: #155724;
}

.activity-status.warning {
  background: #fff3cd;
  color: #856404;
}

.activity-status.info {
  background: #d1ecf1;
  color: #0c5460;
}

/* 快速操作样式 */
.quick-actions-section {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.quick-actions {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.quick-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.quick-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.btn-icon {
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .home-page {
    padding: 10px;
  }

  .page-title {
    font-size: 28px;
  }

  .page-subtitle {
    font-size: 16px;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .menu-grid {
    grid-template-columns: 1fr;
  }

  .quick-actions {
    flex-direction: column;
  }

  .quick-btn {
    width: 100%;
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }

  .stat-card {
    padding: 16px;
  }

  .menu-card {
    padding: 20px;
  }
}
</style>
